package com.starry.security.security;

import com.starry.utils.utils.R;
import com.starry.utils.utils.ResponseUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TokenLogoutHandler implements LogoutHandler {

	private TokenManager tokenManager;
	private RedisTemplate redisTemplate;
	public TokenLogoutHandler(TokenManager tokenManager,RedisTemplate redisTemplate) {
		this.tokenManager = tokenManager;
		this.redisTemplate = redisTemplate;
	}

	@Override
	public void logout(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) {
		//从header里面获取token
		String token = httpServletRequest.getHeader("token");
		if (token != null) {
			//移除
			tokenManager.removeToken(token);
			String username = tokenManager.getUserInfoFromToken(token);
			//token不为空 移除token 从redis删除token
			redisTemplate.delete(username);

		}

		ResponseUtil.out(httpServletResponse, R.ok());
	}
}
